package org.farrell.EOSCalc;

import org.farrell.EOSCalc.CompDbAdapter;
import org.farrell.EOSCalc.R;

import android.app.Activity;
import android.database.Cursor;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;

public class CompEdit extends Activity {
	private EditText mCompNameText;
	private EditText mPressureText;
    private EditText mTemperatureText;
    private EditText mAcentricText;
    private Long mRowId;
    
    private CompDbAdapter mDbHelper;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        
        mDbHelper = new CompDbAdapter(this);
        mDbHelper.open();
        
        setContentView(R.layout.comp_edit);
        setTitle(R.string.edit_comp);

        mCompNameText = (EditText) findViewById(R.id.comp_name);
        mPressureText = (EditText) findViewById(R.id.pressure);
        mTemperatureText = (EditText) findViewById(R.id.temperature);
        mAcentricText = (EditText) findViewById(R.id.acentric);

        Button confirmButton = (Button) findViewById(R.id.confirm);

        mRowId = (savedInstanceState == null) ? null :
            (Long) savedInstanceState.getSerializable(CompDbAdapter.KEY_ROWID);
        if (mRowId == null) {
            Bundle extras = getIntent().getExtras();
            mRowId = extras != null ? extras.getLong(CompDbAdapter.KEY_ROWID)
                                    : null;
        }
        
        populateFields();

        confirmButton.setOnClickListener(new View.OnClickListener() {

            public void onClick(View view) {
                setResult(RESULT_OK);
                finish();
            }

        });
    }
    
    private void populateFields() {
        if (mRowId != null) {
            Cursor compound = mDbHelper.fetchComp(mRowId);
            startManagingCursor(compound);
            mCompNameText.setText(compound.getString(
                        compound.getColumnIndexOrThrow(CompDbAdapter.KEY_NAME)));
            double critP = compound.getDouble(compound.getColumnIndexOrThrow(CompDbAdapter.KEY_CRIT_PRES))*1e-6;
            double critT = compound.getDouble(compound.getColumnIndexOrThrow(CompDbAdapter.KEY_CRIT_TEMP));
            double acent = compound.getDouble(compound.getColumnIndexOrThrow(CompDbAdapter.KEY_ACENTRIC));
            mPressureText.setText(Double.toString(critP));
            mTemperatureText.setText(Double.toString(critT));
            mAcentricText.setText(Double.toString(acent));
        }
    }
    
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);
        saveState();
        outState.putSerializable(CompDbAdapter.KEY_ROWID, mRowId);
    }
    
    @Override
    protected void onPause() {
        super.onPause();
        saveState();
    }
    
    @Override
    protected void onResume() {
        super.onResume();
        populateFields();
    }
    
    private void saveState(){
    	double pressure, temperature, acentric;
    	String comp_name, temp;
    	comp_name = mCompNameText.getText().toString();
    	if (comp_name.trim().length()==0){
    		// do nothing
//    		Toast toast = Toast.makeText(getApplicationContext(), "empty string", Toast.LENGTH_LONG);
//    		toast.show();
    	} else {

    		try {
    			temp = mPressureText.getText().toString();
    			pressure = Double.parseDouble(temp)*1e6;
    		}
    		catch (Exception e){
    			pressure = 0.0;
    		}
    		try{
    			temp = mTemperatureText.getText().toString();
    			temperature = Double.parseDouble(temp);
    		}
    		catch (Exception e){
    			temperature = 0.0;
    		}
    		try{
    			temp = mAcentricText.getText().toString();
    			acentric = Double.parseDouble(temp);
    		}
    		catch (Exception e){
    			acentric = 0.0;
    		}

    		if (mRowId == null) {
    			long id = mDbHelper.createComp(comp_name, temperature, pressure, acentric);
    			if (id > 0) {
    				mRowId = id;
    			}
    		} else {
    			mDbHelper.updateComp(mRowId, comp_name, temperature, pressure, acentric);
    		}
    	}
    }
}


